﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Portal.Models.SkillsCatalog;
using Portal.Logic.Validators.SkillsCatalog;
using Portal.Model;
using Portal.Repository;
using Portal.Repository.SkillsCatalog;
using RepositoryFramework.Services;
using RepositoryFramework;

namespace Portal.Logic.Services.SkillsCatalog
{
	/// <summary>
	/// Class <see cref="CurriculumVitaeService"/> declare curriculum vitae service
	/// </summary>
	public class CurriculumVitaeService : ServiceBase<IUnitOfWorkProvider<ISkillsCatalogUnitOfWork>, ISkillsCatalogUnitOfWork, ICurriculumVitae>, ICurriculumVitaeService
	{
		#region Fields
		private readonly ICurriculumVitaeRepository _repository;
		#endregion

		#region .ctor
		/// <summary>
		/// Class <see cref="CurriculumVitaeService"/> constructor
		/// </summary>
		/// <param name="unitOfWork"></param>
		public CurriculumVitaeService(ISkillsCatalogUnitOfWorkProvider provider, ICurriculumVitaeValidator validator)
			: base(provider, validator)
		{
			if (GetReadOnlyUnitOfWork().CurriculumVitaeRepository == null)
			{
				throw new ArgumentNullException("CurriculumVitaeRepository");
			}
			_repository = GetReadOnlyUnitOfWork().CurriculumVitaeRepository;
		}
		#endregion

		#region Methods

		/// <summary>
		/// Gets the cv by user.
		/// </summary>
		/// <param name="user">The user.</param>
		/// <returns>
		/// CV
		/// </returns>
		public ICurriculumVitae GetBy(IUser user)
		{
			if (user == null)
			{
				return null;
			}
			return _repository.GetByUser(user.Id);
		}

		/// <summary>
		/// Gets the cv preview by user.
		/// </summary>
		/// <param name="user">The user.</param>
		/// <returns>CV preview</returns>
		public ICurriculumVitaePreview GetPreviewBy(IUser user) {
			if (user == null)
			{
				return null;
			}
			return _repository.GetPreviewByUser(user.Id);
		}

		/// <summary>
		/// Determines whether cv is up to date on the specified date.
		/// </summary>
		/// <param name="cv">The cv.</param>
		/// <param name="onDate">The date.</param>
		/// <returns></returns>
		public bool IsUpToDate(ICurriculumVitae cv, DateTime onDate)
		{
			if (cv == null)
			{
				throw new ArgumentNullException("cv");
			}
			return (onDate - cv.UpdatedOn).Days <= 60;
		}

		#endregion
	}
}
